<?php

namespace app\admin\model;

use app\common\library\RedisService;
use think\Model;


class Equipment extends Model
{





    // 表名
    protected $name = 'equipment';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        "source_full",
        'code_full',
        'font_size',
        'font_size_en'
    ];

    public function getFontSizeAttr($value, $data)
    {
        return doubleval($value);
    }
    public function getFontSizeEnAttr($value, $data)
    {
        return doubleval($value);
    }

    public function getSourceFullAttr($value, $data)
    {
        return $data['source_file']?cdnurl($data['source_file'],true):"";
    }
    public function getCodeFullAttr($value, $data){
        return $data['code_image']?cdnurl($data['code_image'],true):"";
    }

    public function getStatusList()
    {
        return ['online' => __('在线'), 'offline' => __('离线')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $redis=new RedisService();
        $last_time=$redis->getHash("equipment_last_request_time",$data['station']);
        $list = $this->getStatusList();
        if($last_time && time()-$last_time>600){
            $equi=self::get($data['id']);
            $equi->status="offline";
            $equi->save();
            return $list['offline'];
        }elseif($data['status']=='offline'){
            $equi=self::get($data['id']);
            $equi->status="online";
            $equi->save();
        }
        $value = $value ?: ($data['status'] ?? '');

        return $list[$value] ?? '';
    }




}
